package kau.dao;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.ibatis.session.SqlSession;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.Reply;
import kau.vo.SmsList;

public class SmsListDAOImpl implements SmsListDAOIF {
	DBUtil db = null;
	private static SmsListDAOImpl instance;
	
	private SmsListDAOImpl(){
		db = DBUtil.getInstance();
	}
	
	public static SmsListDAOImpl getInstance(){
		if(instance == null) instance = new SmsListDAOImpl();
		return instance;
	}
	
	private boolean isExist(int smsId, int courseId){
		HashMap<String,Integer> info = new HashMap<String,Integer>();
		info.put("smsId", smsId);
		info.put("courseId", courseId);
		// info : String SmsId, String CourseId
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		try{
			result = session.selectOne("dbConfig.SmsListMapper.isExist", info);
		}finally{
			session.close();
		}
		if(result!= null) flag= true;
		
		return flag;
		
	}
	@Override
	public void insert(SmsList smsList) throws DuplicationException {

		if(isExist(smsList.getSmsId(),smsList.getCourseId())) throw new DuplicationException(smsList);
		SqlSession session = db.getSession();
		try{
			session.insert("dbConfig.SmsListMapper.insert", smsList);
			session.commit();
		}finally{
			session.close();
		}
	}

	@Override
	public void update(SmsList smsList) throws RecordNotFoundException {
		if(!isExist(smsList.getSmsId(),smsList.getCourseId())) throw new RecordNotFoundException(smsList);
		SqlSession session = db.getSession();
		try{
			session.update("dbConfig.SmsListMapper.update", smsList);
			session.commit();
		}finally{
			session.close();
		}

	}

	@Override
	public void delete(int smsId, int courseId)
			throws RecordNotFoundException {

		HashMap<String,Integer> info = new HashMap<String,Integer>();
		info.put("smsId", smsId);
		info.put("courseId", courseId);
		if(!isExist(smsId,courseId)) throw new RecordNotFoundException(smsId+"가 존재하지않습니다.");
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.SmsListMapper.delete", info);
			session.commit();
		}finally{
			session.close();
		}

	}

	@Override
	public SmsList getSmsList(int smsId, int courseId)
			throws RecordNotFoundException {
		
		if(!isExist(smsId,courseId)) throw new RecordNotFoundException("해당 smsList가 존재하지않습니다");
		SqlSession session = db.getSession();
		SmsList smsList_ret = null;
		HashMap<String, Integer> info = new HashMap<String,Integer>();
		info.put("smsId", smsId);
		info.put("courseId", courseId);
		try{
			smsList_ret = session.selectOne("dbConfig.SmsListMapper.getSmsList", info);
			session.commit();
		}finally{
			session.close();
		}
		return smsList_ret;
	}

	@Override
	public int getMaxSmsId() {
		int number = 0;
		SqlSession session = db.getSession();
		try {
			Integer temp = session.selectOne("dbConfig.SmsListMapper.getMaxSmsId");
			if(temp!=null)number=temp;
		} finally {
			if (session != null)
				session.close();
		}
		return number;
	}

	@Override
	public ArrayList getAllSmsList(int courseId) {
		ArrayList smsList = new ArrayList();
		SqlSession session = db.getSession();
		try{
			smsList =(ArrayList) session.selectList("dbConfig.SmsListMapper.getAllSmsList",courseId);
		} finally{
			if(session!=null) session.close();
		}
		return smsList;
	}

}
